package view;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import model.Richting;
import model.Opties;
/**
 *
 * @author Alvin & Roy
 */
class ButtonListener implements ActionListener {
  /** De main */
  private Main main;

  /**
   * Constructor
   * @param m De main
   */
  public ButtonListener(Main m) {
      this.main = m;
  }

  /**
   * @see ActionListener#actionPerformed(java.awt.event.ActionEvent) Action
   */
  public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("NEW GAME")) {
        main.stop();
        SpelOmgeving spelOmgeving = new SpelOmgeving(Opties.getXSize(),Opties.getYSize(),Opties.getBObstakels(),Opties.getOObstakels(), main);

        main.makeNewSpelOmgeving(spelOmgeving);

        spelOmgeving.requestFocus();

        main.setGameStartStatus(true);

    }
    else if (e.getActionCommand().equals("PAUZE")) {

        if(main.gameStarted()){
            SpelOmgeving s = main.getSpelOmgeving();

            s.togglePauze();

            s.requestFocus();
        }

    }
    else if (e.getActionCommand().equals("STOP")) {
        main.stop();
    }
    else if (e.getActionCommand().equals("Afsluiten")){
        System.exit(0);
    }
    else if (e.getActionCommand().equals("Visualiseer route")){
        Opties.setShowPath(!Opties.getShowPath());
        if(main.gameStarted()){
            SpelOmgeving.toggleShowPath();
        }
    }
    else if (e.getActionCommand().equals("^")){
        if(main.gameStarted()){
            main.moveSpeler(Richting.N);
        }
    }
    else if (e.getActionCommand().equals("v")){
        if(main.gameStarted()){
            main.moveSpeler(Richting.Z);
        }
    }
    else if (e.getActionCommand().equals("<")){
        if(main.gameStarted()){
            main.moveSpeler(Richting.W);
        }
    }
    else if (e.getActionCommand().equals(">")){
        if(main.gameStarted()){
            main.moveSpeler(Richting.O);
        }
    }else if(testInt(e.getActionCommand())){
        for(int i = 1; i<=Opties.getAantalMoeilijkheden(); i++){
            try{
                if(Integer.parseInt(e.getActionCommand()) == i){
                    Opties.setMoeilijkheidsgraad(i);
                }
            }catch(NumberFormatException nfe){
                
            }
        }
    }
    else if(e.getActionCommand().equals("Ronde wereld")){
        Opties.setRanden(!Opties.getRanden());
    }
  }

  /**
   * Test of een string een integer bevat
   * @param s De string die getest wordt
   * @return True als er geen exception op treedt
   */
  private boolean testInt(String s){
      try{
        Integer.parseInt(s);
      }catch(NumberFormatException nfe){
        return false;
      }
      return true;
  }
}
